var App = function () {
    var alertDialog = function (msg, title) {
        if (title == '' || title == undefined) {
            title = '操作提醒';
        }
        var dialog = '<div class="weui_dialog_alert" id="dialog">';
        dialog += '<div class="weui_mask"></div>';
        dialog += ' <div class="weui_dialog">';
        dialog += '    <div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div>';
        dialog += '  <div class="weui_dialog_bd">' + msg + '</div>';
        dialog += '  <div class="weui_dialog_ft">';
        dialog += '       <a href="javascript:;" class="weui_btn_dialog primary">确定</a>';
        dialog += '     </div>';
        dialog += '  </div>';
        dialog += ' </div>';
        $("body").append(dialog);
        $("#dialog").find('.weui_btn_dialog').on('click', function () {
            $("#dialog").remove();
        });
    }
    var confirmDialog = function (msg, title) {
        if (title == '' || title == undefined) {
            title = '操作提醒';
        }
        var dialog = '<div class="weui_dialog_alert" id="dialog1">';
        dialog += '<div class="weui_mask"></div>';
        dialog += ' <div class="weui_dialog">';
        dialog += '    <div class="weui_dialog_hd"><strong class="weui_dialog_title">' + title + '</strong></div>';
        dialog += '  <div class="weui_dialog_bd">' + msg + '</div>';
        dialog += '  <div class="weui_dialog_ft">';
        dialog += '       <a href="javascript:;" class="weui_btn_dialog primary">取消</a>';
        dialog += '       <a href="javascript:;" class="weui_btn_dialog primary" id="btnConfirm">确定</a>';
        dialog += '     </div>';
        dialog += '  </div>';
        dialog += ' </div>';
        $("body").append(dialog);
        $("body").find('.weui_btn_dialog').one('click', function () {
            $("#dialog1").remove();
        });
    }
    var showError = function (msg) {
        $('.js_tooltips').html(msg);
        var $tooltips = $('.js_tooltips');
        if ($tooltips.css('display') != 'none') {
            return;
        }
        $tooltips.show();
        setTimeout(function () {
            $tooltips.hide();
        }, 2000);
    }
    var setModelValidate = function () {

        var showValidationSummaryDialog = function () {
            var msg_error = $('.validation-summary-errors').html();
            if (msg_error != undefined) {
                showError(msg_error);
            }
            $('.validation-summary-errors').hide();
        }
        showValidationSummaryDialog();
        $('form').bind('invalid-form.validate', function (error, element) {
            showValidationSummaryDialog();
        });
    };
    var showLoading = function (msg) {
        if (msg == '' || msg == undefined) {
            msg = '数据加载中';
        }
        var loadhtml = '<div id="loadingToast" class="weui_loading_toast">';
        loadhtml += '    <div class="weui_mask_transparent"></div>';
        loadhtml += '      <div class="weui_toast">';
        loadhtml += '         <div class="weui_loading">';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_0"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_1"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_2"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_3"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_4"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_5"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_6"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_7"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_8"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_9"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_10"></div>';
        loadhtml += '         <div class="weui_loading_leaf weui_loading_leaf_11"></div>';
        loadhtml += '  </div>';
        loadhtml += '  <p class="weui_toast_content">' + msg + '</p>';
        loadhtml += '</div>';
        loadhtml += '</div>';
        $("body").append(loadhtml);
    }
    var hideLoading = function () {
        $("#loadingToast").remove();
    }
    var showToast = function (msg) {
        if (msg == '' || msg == undefined) {
            msg = '已完成';
        }
        var html = '<div id="toast">';
        html += '      <div class="weui_mask_transparent"></div>';
        html += '      <div class="weui_toast">';
        html += '           <i class="weui_icon_toast"></i>';
        html += '           <p class="weui_toast_content">' + msg + '</p>';
        html += '      </div>';
        html += '  </div>';
        $("body").append(html);
        setTimeout(function () {
            $("#toast").remove();
        }, 2000);
    }
    var search = function () {
        if ($('#search_input').val() != '') {
            var $weuiSearchBar = $('#search_bar');
            $weuiSearchBar.addClass('weui_search_focusing');
        }
        $('#search_input').bind('focus', function () {
            var $weuiSearchBar = $('#search_bar');
            $weuiSearchBar.addClass('weui_search_focusing');
        });
        $('#search_input').bind('blur', function () {
            var $weuiSearchBar = $('#search_bar');
            $weuiSearchBar.removeClass('weui_search_focusing');
            if ($(this).val()) {
                $('#search_text').hide();
            } else {
                $('#search_text').show();
            }
        });
        $('#search_cancel').bind('click', function () {
            $("#search_show").hide();
            $('#search_input').val('');
            $('.weui_search_outer').submit();
        });
        //$('#search_clear').bind('click', function () {
        //    $("#search_show").hide();
        //    $('#search_input').val('');
        //});
    }
    var scrollLoad = function () {
        $('#container').infinitescroll({
            navSelector: "#navigation",
            nextSelector: "#navigation a",
            itemSelector: ".item_box",
            loading: {
                start: showLoading(),
                finished: hideLoading(),
                finishedMsg: "",
                //img: "",
                //msg: null,
                msgText: "<em>加载中...</em>",
            }
        });
    }
    return {
        showError: showError,
        setModelValidate: setModelValidate,
        showLoading: showLoading,
        hideLoading: hideLoading,
        showToast: showToast,
        alertDialog: alertDialog,
        confirmDialog: confirmDialog,
        search: search,
        scrollLoad: scrollLoad
    }
}();


var onBegin = function () {
    var flag = true;
    $('[require]').each(function (index, item) {
        if ($(item).val().trim() == '') {
            flag = false;
            App.alertDialog($(item).attr('placeholder'));
            return false;
        }
    });
    if (!flag) {
        return false;
    }
    App.showLoading();
}
var onSuccess = function (data) {
    App.hideLoading();
    switch (data.code) {
        case 200:
            if (data.msg == '' || data.msg == null) {
                App.showToast();
            } else {
                App.showToast(data.msg);
            }
            //如果存在Url一秒后跳转
            if (data.url != null && data.url != '') {
                setTimeout(function () {
                    window.location.href = data.url;
                }, 1000);
            }
            break;
        case 500:
            App.alertDialog(data.msg);
            //如果存在Url一秒后跳转
            if (data.url != null && data.url != '') {
                setTimeout(function () {
                    window.location.href = data.url;
                }, 1000);
            }
            break;
        default:
    }
}


//图片上传
var ajaxUploadImg = function (btn) {
    $(".weui_uploader_file").live("click", function () {
        $(this).remove();
    });

    var $button = btn;
    new AjaxUpload($button, {
        action: '/OAuth/UploadFile/?dir=image',
        name: 'imgFile',
        dataType: 'json',
        onSubmit: function (file, ext) {
            //if (!(ext && /^(jpeg|jpg|png|png|gif)$/i.test(ext))) {
            //    alert("您上传的图片格式不对(" + ext + ")，请重新选择！");
            //    return false;
            //}

            //大小控制
            try {
                var max = 5;
                var max_size = max * 1024 * 1024;
                var files = this._input.files; // 获得文件对象
                for (var i = 0, f; f = files[i]; i++) {  //检查文件大小
                    if (f.size > max_size) {
                        alert("上传的图片不能超过" + max + "M!");
                        return false;
                    }
                }
            } catch (e) {

            }

            App.showLoading();
        },
        onComplete: function (file, data) {
            App.hideLoading();
            var jsonData = JSON.parse(data);
            if (jsonData["error"] != 0) {
                alert(jsonData['message']);
            } else {
                var imgUrl = jsonData["url"].replace('@@800w', '');
                var html = '<li class="weui-uploader__file weui-uploader__file_status weui_uploader_file" url="' + imgUrl + '" style="background-image: url(' + imgUrl.replace('w_800','w_375') + ')">';
                html += '   <div class="del_b">删除</div>';
                html += '   </li>';
                var $files = $(btn).closest('.weui_uploader_bd').find('.weui_uploader_files');
                $($files).append(html);
            }
        }
    });
}


//人脸照片上传
//图片上传
var ajaxUploadFace = function (btn) {
    $(".weui_uploader_file").live("click", function () {
        $(this).remove();
        btn.show();
    });
    var $button = btn;
    new AjaxUpload($button, {
        action: '/OAuth/UploadFaceImg/?dir=image',
        name: 'imgFile',
        dataType: 'json',
        onSubmit: function (file, ext) {
            //if (!(ext && /^(jpeg|jpg|png|png|gif)$/i.test(ext))) {
            //    alert("您上传的图片格式不对(" + ext + ")，请重新选择！");
            //    return false;
            //}

            //大小控制
            try {
                var max = 10;
                var max_size = max * 1024 * 1024;
                var files = this._input.files; // 获得文件对象
                for (var i = 0, f; f = files[i]; i++) {  //检查文件大小
                    if (f.size > max_size) {
                        alert("上传的图片不能超过" + max + "M!");
                        return false;
                    }
                }
            } catch (e) {

            }

            App.showLoading();
        },
        onComplete: function (file, data) {
            App.hideLoading();
            var jsonData = JSON.parse(data);
            if (jsonData["error"] != 0) {
                alert(jsonData['message']);
            } else {
                var imgUrl = jsonData["url"].replace('@@800w', '');
                var html = '<li class="weui-uploader__file weui-uploader__file_status weui_uploader_file" url="' + imgUrl + '" style="background-image: url(' + imgUrl.replace('w_800', 'w_375') + ')">';
                html += '   <div class="del_b">删除</div>';
                html += '   </li>';
                var $files = $(btn).closest('.weui_uploader_bd').find('.weui_uploader_files');
                $($files).append(html);

                btn.hide();
            }
        }
    });
}